﻿using System;
using CoreVirtuoso.Helpers;
using OpenLink.Data.Virtuoso;
using VirtuosoDal.Repositories.RdfRepositories.Interfaces.VirtuosoCore;

namespace VirtuosoDal.Repositories.RdfRepositories.VirtuosoCore
{
   public class SequenceRepository : ISequenceRepository
   {
      public int SequenceSet(string sequenceName, int initialValue)
      {
         string connectionString = ConnectionStringsHelper.GetVirtuosoConnectionString();
         using (var connection = new VirtuosoConnection(connectionString))
         {
            var cmd = new VirtuosoCommand();
            cmd.CommandText = String.Format("SELECT sequence_set('{0}', {1}, 0)", sequenceName, initialValue);
            cmd.Connection = connection;

            connection.Open();
            int result = (int) cmd.ExecuteScalar();
            connection.Close();

            return result;
         }
      }

      public int SequenceNext(string sequenceName)
      {
         string connectionString = ConnectionStringsHelper.GetVirtuosoConnectionString();
         using (var connection = new VirtuosoConnection(connectionString))
         {
            var cmd = new VirtuosoCommand();
            cmd.CommandText = String.Format("SELECT sequence_next('{0}', 1)", sequenceName);
            cmd.Connection = connection;

            connection.Open();
            int result = (int) cmd.ExecuteScalar();
            connection.Close();

            return result;
         }
      }
   }
}